<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>生命周期</title>
</head>
<body>
    <!-- 准备一个容器 -->
    <div id="test"></div>

    <!-- 引入react核心库 -->
    <script type="text/javascript" src="../js/react.development.js"></script>
    <!-- 引入react-dom，支持react操作dom -->
    <script src="../js/react-dom.development.js"></script>
    <!-- 引入babel，将jsx转为js -->
    <script src="../js/babel.min.js"></script>
    <!-- 引入prop-types ，对标签属性进行限制 -->
    <script src="../js/prop-types.js"></script>

    <script type="text/babel">/*此处写babel*/ 
        class Life extends React.Component{
            //初始化状态
            state = {
                opacity:1
            }

            //初始化渲染、状态更新后
            render() {
                return(
                    <div>
                        <h1 style={{opacity:this.state.opacity}}>look at this</h1>
                        <button onClick={this.death}>不活了</button>
                    </div>   
                )
            }

            //组件挂载完毕
            componentDidMount() {
                this.timer = setInterval(() => {
                    let {opacity} = this.state;
                    opacity -= 0.1;
                    if(opacity<=0) opacity=1;
                    this.setState({opacity});
                }, 200);
            }

            //组件将要卸载
            componentWillUnmount(){
                clearInterval(this.timer);
            }

            death = ()=>{
                //卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('test'));
            }
        }

        ReactDOM.render(<Life/>,document.getElementById('test'))
    </script>
</body>
</html>